package com.foodplus.www.adapter.basket;

import android.content.Context;
import android.view.View;
import android.widget.Button;

import com.foodplus.www.bean.basket.BasketBean;
import com.foodplus.www.bean.basket.CartListBean;
import com.foodplus.www.bean.basket.GoodsBean;
import com.foodplus.www.foodplus.MyApp;
import com.foodplus.www.foodplus.R;
import com.foodplus.www.utils.BaseRecyclerViewAdapter;

import java.util.List;

/**
 * Created by WangZhiChao on 2016/11/2.
 */

public class BasketAdapter extends BaseRecyclerViewAdapter<BasketBean> {

    private OnChangeNumListener listener;
    public static final int TYPE_SECTION_HEADER = 1;
    public static final int TYPE_ITEM = 2;

    List<CartListBean> cartList;
    BasketBean data;


    public BasketAdapter(BasketBean data) {
        super(null);
        this.data = data;
        if (data != null) {
            this.cartList = data.getDatas().getCart_list();
        }

    }


    @Override
    public int getDataCount() {
        int carListCount = getCartListCount();
        int goodCount = getGoodCount();
        return carListCount + goodCount;
    }


    @Override
    protected void initItemView(Context context, BaseViewHolder holder, BasketBean data, int realPosition) {
        int headerIndex = 0;
        if ((headerIndex = isHead(realPosition)) < 0) {//判断是否是店铺头部

            //判断是否处于编辑状态
            if (MyApp.getInstance().isBasketEditting()) {//如果是,显示编辑窗口
                holder.getViewById(R.id.ll_good_amount).setVisibility(View.VISIBLE);
                holder.getViewById(R.id.ll_good_title).setVisibility(View.GONE);
            }else {//如果否,只显示结果
                holder.getViewById(R.id.ll_good_amount).setVisibility(View.GONE);
                holder.getViewById(R.id.ll_good_title).setVisibility(View.VISIBLE);
            }


            //获取当前商品数据,并显示
            final GoodsBean item = getItem(realPosition);
            holder.loadImageFromUrl(R.id.iv_good, item.getGoods_image_square());
            holder.setText(R.id.tv_count, "x"+item.getGoods_num());
            holder.setText(R.id.tv_count1, item.getGoods_num());
            holder.setText(R.id.tv_good_name, item.getGoods_name());
            holder.setText(R.id.tv_price, "¥ " + item.getGoods_price());
            //添加商品
            Button btnAdd = (Button) holder.getViewById(R.id.btn_add);
            btnAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(listener!=null){
                        listener.onAddNumListener(item);
                    }
                }
            });
            //减少商品
            Button btnMinus = (Button) holder.getViewById(R.id.btn_minus);
            btnMinus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(listener!=null){
                        listener.onMinusNumListener(item);
                    }
                }
            });

        } else {//如果是店铺头部
            CartListBean cartListBean = this.data.getDatas().getCart_list().get(headerIndex);//获取店铺信息
            holder.setText(R.id.tv_shop_name, cartListBean.getStore_name());
        }
    }

    public void setChangeNumListener(OnChangeNumListener listener) {
        this.listener = listener;
    }


    public interface OnChangeNumListener {
        void onAddNumListener(GoodsBean item);
        void onMinusNumListener(GoodsBean item);
    }


    @Override
    public int getItemLayoutId(int viewType) {
        switch (viewType) {
            case TYPE_SECTION_HEADER:
                return R.layout.item_basket_header;
            default:
                return R.layout.item_basket_good;
        }
    }


    public int getCartListCount() {
        return cartList != null ? cartList.size() : 0;
    }

    public int getGoodCount() {
        int goodCount = 0;
        if (cartList != null) {
            for (CartListBean cartListBean : cartList) {
                if (cartListBean.getGoods() != null) {
                    goodCount++;
                }
            }
        }
        return goodCount;
    }


    @Override
    protected int myGetItemViewType(int realPosition) {
        if (isHead(realPosition) >= 0) {
            return TYPE_SECTION_HEADER;
        } else {
            return TYPE_ITEM;
        }
    }


    //判断是否是头部,如果是,返回对应的index,不是,返回-1
    public int isHead(int position) {
        int temp = position;
        int carListCount = getCartListCount();
        int i = 0;

        while (temp >= 0 && i < carListCount) {
            if (temp == 0) {
                return i;
            }
            temp -= (cartList.get(i).getGoods().size() + 1);
            i++;
        }
        return -1;
    }

    public GoodsBean getItem(int position) {
        int temp = position;
        int i = 0;

        for (CartListBean cartListBean : cartList) {
            if (cartList.get(i).getGoods().size() > (--temp)) {
                break;
            } else {
                temp -= cartList.get(i).getGoods().size();
                i++;
            }
        }
        return cartList.get(i).getGoods().get(temp);
    }

    public void refreshData(BasketBean data) {
        this.data = data;
        if (data != null) {
            this.cartList = data.getDatas().getCart_list();
        }
        this.notifyDataSetChanged();
    }
}
